//整体流程参照顺序表
#include <iostream>
using namespace std;
typedef struct
{
	int x, y;
}ElemType;

typedef struct LNode
{
	ElemType data;
	struct LNode *next;
}LNode, *LinkList;

void Init(LinkList &L)
{
	L = new LNode;
	L->next = NULL; 
}
void CH(LinkList &L, int n) // 头插 
{
	while(n--)
	{
		LNode *p = new LNode;
		cin >> p->data.x >> p->data.y;
		p->next = L->next;
		L->next = p; 
	}
} 
void CT(LinkList &L, int n) // 尾插
{
	LNode *T = L;
	while(T->next)
	{
		T = T->next;
	}
	while(n--)
	{
		LNode *p = new LNode;
		cin >> p->data.x >> p->data.y;
		p->next = T->next;
		T->next = p;
		T = p;
	}
}
void Get(LinkList L, int w, ElemType &e) // 取值 
{
	LNode *p = L->next;
	int j = 1; 
	while(p && j < w)
	{
		p = p->next;
		j++;
	}
	e = p->data;
}
LNode* Locate(LinkList L, ElemType e)
{
	LNode *p = L->next;
	while(p && (p->data.x != e.x || p->data.y != e.y))
	{
		p = p->next;
	}
	return p;
}
void Insert(LinkList &L, int w, ElemType e)
{
	LNode *p = L;
	int j = 0;
	while(p && j < w - 1)
	{
		p = p->next;
		j++; 
	}
	LNode *q = new LNode;
	q->data = e;
	q->next = p->next;
	p->next = q;
}
void Delete(LinkList &L, int w)
{
	LNode *p = L;
	int j = 0;
	while(p->next && j < w - 1)
	{
		p = p->next;
		j++;
	}
	LNode *q = p->next;
	p->next = q->next;
	delete q;
}
void Show(LinkList L)
{
    LNode *p = L->next;
    while(p)
    {
        cout << p->data.x << " " << p->data.y << endl;
        p = p->next;
    }
}
int main()
{
	LinkList L;
	Init(L);
	int n, num, w; cin >> n;
	ElemType e;
    LNode *p = new LNode;
	while(n--)
	{
		char op; cin >> op;
		switch(op)
		{
			case 'H':
				cin >> num;
				CH(L, num);
				break;
			case 'T':
				cin >> num;
				CT(L, num);
				break;
			case 'G':
				cin >> w;
				Get(L, w, e);
				cout << e.x << " " << e.y << endl;
				break;
			case 'L':
				cin >> e.x >> e.y;
				p = Locate(L, e);
				cout << p->data.x << " " << p->data.y << endl;
				break;
			case 'I':
				cin >> w;
				cin >> e.x >> e.y;
				Insert(L, w, e);
				break;
			case 'D':
				int w; cin >> w;
				Delete(L, w);
				break;
		}
	}
	Show(L); 
	return 0;
}
	
